import 'package:assistant_zhao/booking_page.dart';
import 'package:assistant_zhao/home_page.dart';
import 'package:assistant_zhao/setting_page.dart';
import 'package:flutter/material.dart';
import 'package:mpflutter_wechat_webview/mpflutter_wechat_webview.dart';

class MyHomeTabPage extends StatefulWidget {
  const MyHomeTabPage({super.key, required this.title});

  final String title;

  @override
  State<MyHomeTabPage> createState() => _MyHomeTabPageState();
}

class _MyHomeTabPageState extends State<MyHomeTabPage> {

  int _tabIndex = 0;

  final Widget _homePage = HomePage();
  final Widget _bookingPage = BookingPage();
  final Widget _settingPage = SettingPage(); 

  List<Widget> get pageList => <Widget>[
    _homePage,
    _bookingPage,
    _settingPage
  ];

  late List<Widget> _cachePageList;

  @override
  void initState() {
    super.initState();
    _cachePageList = List<Widget>.generate(pageList.length, (_) => Container());
    _cachePageList[0] = pageList[0];
  }

  Widget _buildBodyPage() {
    return IndexedStack(
      index: _tabIndex,
      children: _cachePageList,
    );
  }

  void toChatBotDemo() {
    final String webViewID = MPFlutter_Wechat_WebView.open(
          "https://iac-uat.oocl.com/chatui/#/app/online?tntInstId=HSYQOOCL&channel=FS&scene=SCE0000012&sign=bb044b081b6fdb17be7737d8878189ef&key=qUQ%2B1DSoCe9sRhIZ%2BpGzcrjNoaJa21p1aP9%2F1cbs%2B%2FTWiwHfAta18AgqN5butP3vuCWaOq5MtESFZY98dK5waf7nRcEa3QQUzYhtHpXS97a6K686RV9ogn007%2BEDs9PHKUsCfGVepyLD77wNwZU%2FkGriQ0i2HbxmsUtbgV1zNPs%3D&extendInfo=XERcsNOqLE4jZ2EbOCRifrbc3yfP112D2AQF6MAfQactLdQC5Un5%2FxjpaLmrFN59",
          onLoad: (_) {
          print("webview loaded");
        },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xffE22E4F),
        title: Text(widget.title),
        leading: SizedBox.shrink(),
      ),
      body: _buildBodyPage(),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(),
        child: BottomNavigationBar(
            items: getMenuList(),
            currentIndex: _tabIndex,
            onTap: switchTab,
            selectedItemColor: Color(0xffE22E4F),
          ),
      ),
      floatingActionButton: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          toChatBotDemo();
        },
        child: SizedBox(
          width: 100,
          child: Image.asset(
            'assets/images/fab_idle_schi.png',
          )
        ),
      ),
      );
  }

  List<BottomNavigationBarItem> getMenuList() {
    return <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: '首页'
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.book),
        label: '订单'
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.settings),
        label: '设置'
      )
    ];
  }

  void switchTab(int index) {
    if (_tabIndex != index) {
      setState(() => _tabIndex = index);
      onTabChanged(_tabIndex);
    }
  }

  void onTabChanged(int index) {
    final Widget page = pageList[index];
    if (!_cachePageList.contains(page)) {
      _cachePageList[index] = pageList[index];
    }
  }

}
